<?php

namespace app\admin\controller;

use app\common\controller\Backend;

class Post extends Backend
{
    public function postlist()
    {
        $where = null;
        $postList = null;
        if ($this->request->isGet()) {
            $params = $this->request->param();
            $name = !empty($params['search_field']) ? $params['search_field'] : '';
            $keyword = !empty($params['keyword']) ? $params['keyword'] : '';

            if ($name == 'title') {
                $where = "1";
                $postList = model('Post.Post')->with(['cate', 'user'])->where('title', 'like', "%$keyword%")->paginate(7);
                // echo model('Admin')->getlastsql();
                // exit;
            }
            if ($name == 'nickname') {
                $where = "1";
                $userid = model('User.User')->where('nickname', 'like', "%$keyword%")->value('id');
                // halt($userid);
                $postList = model('Post.Post')->with(['cate', 'user'])->where('userid', 'like', "%$userid%")->paginate(7);
            }
            if ($where == null) {
                $postList = model('Post.Post')->with(['cate', 'user'])->paginate(7);
            }
        }

        // $postList = model('Post.Post')->with(['cate', 'user'])->paginate(7);
        $navTitle = '问答管理';
        $navSmall = '帖子列表';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'postList' => $postList
        ]);
    }

    //帖子分类
    public function catelist()
    {
        $catelist = model('Post.Cate')->order('weigh', 'desc')->paginate(8);
        $navTitle = '问答管理';
        $navSmall = '分类列表';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'catelist' => $catelist
        ]);
    }

    //分类添加
    public function cateadd()
    {
        if ($this->request->isPost()) {
            $action = $this->request->param('action', '');
            //ajax检查名称
            if ($action == 'name') {
                $resName = model('Post.Cate')->where('name', $this->request->param('name'))->find();
                if ($resName) {
                    $this->error('分类名称已存在');
                } else {
                    $this->success('分类名称可以使用');
                }
            }
            $params = $this->request->param();
            $data = [
                'name' => $params['name'],
                'weigh' => $params['weigh']
            ];

            $result = model('Post.Cate')->insert($data);
            if ($result === FALSE) {
                $this->error(model('Post.Cate')->getError());
            } else {
                $this->success('新增成功', url('admin/post/catelist'));
            }
        }
        $navTitle = '问答管理';
        $navSmall = '分类列表';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall
        ]);
    }

    //分类编辑
    public function cateedit()
    {
        $id = $this->request->param('id', 0);
        $catelist = model('Post.Cate')->find($id);
        if ($this->request->isPost()) {
            $params = $this->request->param();
            $data = [
                'id' => $id,
                'name' => $params['name'],
                'weigh' => $params['weigh']
            ];
            $result = model('Post.Cate')->isUpdate(true)->save($data);
            if ($result === FALSE) {
                $this->error(model('Post.Cate')->getError());
            } else {
                $this->success('更新成功', url('admin/post/catelist'));
            }
        }
        $navTitle = '问答管理';
        $navSmall = '分类列表';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'catelist' => $catelist
        ]);
    }

    //回收站处理
    public function recyclebin()
    {
        //恢复用户数据
        if ($this->request->isPost() && $this->request->isAjax()) {
            $action = $this->request->param('action', '');
            if ($action == 'restore') {
                $list = $this->request->param();
                $deletetimeList = model('Post.Post')->onlyTrashed()->select($list['list']);
                $result = null;
                foreach ($deletetimeList as $key => $item) {
                    // halt($item['id']);
                    $result = model('Post.Post')->update(['id' => $item['id'], 'deletetime' => null]);
                }
                // $data = [];
                // foreach ($deletetimeList as $key => $item) {
                //     $data[$key]['id'] = $item['id'];
                //     $data[$key]['deteletime'] = null;
                // }
                // $result = model('User.User')->isUpdate(true)->saveAll($data);
                if ($result === FALSE) {
                    $this->error(model('Post.Post')->getError());
                } else {
                    $this->success('恢复成功');
                }
            }
        }

        $where = null;
        $postList = null;
        if ($this->request->isGet()) {
            $params = $this->request->param();
            $name = !empty($params['search_field']) ? $params['search_field'] : '';
            $keyword = !empty($params['keyword']) ? $params['keyword'] : '';

            if ($name == 'title') {
                $where = "1";
                $postList = model('Post.Post')->onlyTrashed()->with(['cate', 'user'])->where('title', 'like', "%$keyword%")->paginate(7);
                // echo model('Admin')->getlastsql();
                // exit;
            }
            if ($name == 'nickname') {
                $where = "1";
                $userid = model('User.User')->onlyTrashed()->where('nickname', 'like', "%$keyword%")->value('id');
                // halt($userid);
                $postList = model('Post.Post')->onlyTrashed()->with(['cate', 'user'])->where('userid', 'like', "%$userid%")->paginate(7);
            }
            if ($where == null) {
                $postList = model('Post.Post')->onlyTrashed()->with(['cate', 'user'])->paginate(7);
            }
        }
        // $postList = model('Post.Post')->onlyTrashed()->paginate(7);
        $navTitle = '问答管理';
        $navSmall = '回收站';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'postList' => $postList
        ]);
    }

    public function del()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $action = $this->request->param('action', '');
            $result = null;
            //帖子软删除
            if ($action == 'del') {
                $id = $this->request->param('id', 0);
                $result = model('Post.Post')->destroy($id);
            }
            if ($action == 'checkboxRemove') {
                $list = $this->request->param();
                $result = model('Post.Post')->destroy($list['list']);
            }
            //永久删除
            if ($action == 'PMdel') {
                $id = $this->request->param('id', 0);
                $result = model('Post.Post')->destroy($id, true);
            }
            if ($action == 'PMRemove') {
                $list = $this->request->param();
                $result = model('Post.Post')->destroy($list['list'], true);
            }
            //分类删除
            $cateResult = null;
            if ($action == 'CateDel') {
                $id = $this->request->param('id', 0);
                $cateResult = model('Post.Cate')->destroy($id);
            }
            if ($action == 'CateRemove') {
                $list = $this->request->param();
                $cateResult = model('Post.Cate')->destroy($list['list']);
            }
            if ($cateResult === FALSE) {
                $this->error(model('Post.Cate')->getError());
            } else {
                $this->success('删除成功');
            }
            if ($result === FALSE) {
                $this->error(model('Post.Post')->getError());
            } else {
                $this->success('删除成功');
            }
        }
    }
}
